﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using System.Drawing;
using Silvermoon.Images;
using Silvermoon.Shapes;
using Silvermoon.UI;
using Silvermoon.Utils;
using Silvermoon.Controls.Base;
using Silvermoon.Animations;
using Silvermoon.Windows;
using Silvermoon.Windows.Styles;

namespace Testing
{
    public class BubbleDemo : DemoWindowBase
    {

        public BubbleDemo()
            : base()
        {
            CreateCustomVisibilityManager();

            //            SetTransition(600, TransitionMask.Top, TransitionMask.Top | TransitionMask.Fade);

            VisualStateManager windowManager = CreateWindowManager();

            Color = Color.Transparent;
            //Color = Color.DarkGray;
            Bubble bubble = new Bubble
            {
                Color = Color.FromArgb(32, 32, 32),
                Bounds = new Rectangle(48, 48, 320, 64 + 32),
                TextColor = Color.White,
                ArrowOffset = -50
            };
            {
                BubbleButton s1 = new BubbleButton { Content = "Copy" };
                BubbleButton s2 = new BubbleButton { Content = "Cut" };
                BubbleButton s3 = new BubbleButton { Content = "Paste" };
                bubble.Buttons.Add(s1);
                bubble.Buttons.Add(s2);
                bubble.Buttons.Add(s3);
            }
            Controls.Add(bubble);

            bubble = new Bubble
            {
                Bounds = new Rectangle(48, 48 + 100, 240, 64 + 32),
                Color = Color.Blue,
                ClipColor = PhoneColors.Orange,
                TextColor = Color.White
            };
            {
                BubbleButton s1 = new BubbleButton { Content = SysImages.Cut };
                BubbleButton s2 = new BubbleButton { Content = SysImages.Copy };
                BubbleButton s3 = new BubbleButton { Content = SysImages.Paste };
                bubble.Buttons.Add(s1);
                bubble.Buttons.Add(s2);
                bubble.Buttons.Add(s3);
            }
            Controls.Add(bubble);

            VisualStateManager btnManager = new VisualStateManager(
                new VisualState(VState.ButtonPressed,
                    new ColorTransition { Duration = 5, To = PhoneColors.Orange, Property = Button.ColorProperty }
                    ),
                new VisualState(VState.Normal, 500)
                );


            Color color = Color.Blue;

            Button b1 = new Button
            {
                StateManager = btnManager,
                Bounds = new Rectangle(48, 300, 100, 68),
                Background = new BackgroundShape { Texture = SysImages.LeftRounded, Style = RenderStyle.Plastic },
                Color = color,
                Content = "White",
            };
            Button b2 = new Button
            {
                StateManager = btnManager,
                Bounds = new Rectangle(147, 300, 100, 68),
                Background = new BackgroundShape { Texture = SysImages.NotRounded, Style = RenderStyle.Plastic },
                Color = color,
                Content = "Orange"
            };
            Button b3 = new Button
            {
                StateManager = btnManager,
                Bounds = new Rectangle(246, 300, 100, 68),
                Background = new BackgroundShape { Texture = SysImages.NotRounded, Style = RenderStyle.Plastic },
                Color = color,
                Content = "Blue"
            };
            Button b4 = new Button
            {
                StateManager = btnManager,
                Bounds = new Rectangle(345, 300, 100, 68),
                Background = new BackgroundShape { Texture = SysImages.RightRounded, Style = RenderStyle.Plastic },
                Color = color,
                Content = "Green"
            };

            b1.Tap += (s, e) =>
                {
                    if (Color == Color.Black)
                    {
                        windowManager.GoTo(this, "Light", true);
                        //Color = Color.White;
                        //ToolbarColor = Color.White;
                        Toolbar.TextColor = Color.Black;
                    }
                    else
                    {
                        windowManager.GoTo(this, "Dark", true);
                        //ToolbarColor = Color.Black;
                        //Color = Color.Black;
                        Toolbar.TextColor = Color.White;
                    }
                };

            b2.Tap += (s, e) =>
                {
                    windowManager.GoTo(this, "Orange", true);
                    Toolbar.TextColor = Color.White;
                };

            b3.Tap += (s, e) =>
            {
                windowManager.GoTo(this, "Blue", true);
                Toolbar.TextColor = Color.White;
            };
            b4.Tap += (s, e) =>
            {
                windowManager.GoTo(this, "Green", true);
                Toolbar.TextColor = Color.White;
            };

            Controls.Add(b1, b2, b3, b4);

            bubble = new Bubble
            {
                Bounds = new Rectangle(48, 500, 320, 64 + 32),
                Color = Color.Gainsboro,
                ClipColor = PhoneColors.Orange,
                TextColor = PhoneColors.FromArgb(240, 0, 0, 0),
                ArrowOffset = -48
            };
            {
                BubbleButton s1 = new BubbleButton { Content = "Copy" };
                BubbleButton s2 = new BubbleButton { Content = "Cut" };
                BubbleButton s3 = new BubbleButton { Content = "Paste" };
                bubble.Buttons.Add(s1);
                bubble.Buttons.Add(s2);
                bubble.Buttons.Add(s3);
            }
            Controls.Add(bubble);

            Slider slider = new Slider
            {
                Minimum = -128,
                Maximum = 128,
                Value = 0,
                Bounds = new Rectangle(48, 640, 320, 48)
            };
            slider.ValueChanged += (s, e) =>
                {
                    bubble.ArrowOffset = slider.Value;
                };

            Controls.Add(slider);

            ToggleSwitch ts = new ToggleSwitch
            {
                Bounds = new Rectangle(48, 400, 160, 64),
                On = "Top",
                Off = "Bottom",
                IsChecked = true,
            };
            ts.CheckedChanged += (s, e) =>
                {
                    bubble.ArrowPlacement = ts.IsChecked ? Placement.Bottom : Placement.Top;
                };

            Controls.Add(ts);
        }

        private static VisualStateManager CreateWindowManager()
        {
            VisualStateManager windowManager = new VisualStateManager(
                new VisualState("Dark",
                    new ColorTransition
                    {
                        Property = Window.ColorProperty,
                        Duration = 500,
                        To = Color.Black
                    }
                    ),
                new VisualState("Light",
                    new ColorTransition
                    {
                        Property = Window.ColorProperty,
                        Duration = 500,
                        To = Color.White
                    },
                    new ColorTransition
                    {
                        Property = Window.ToolbarColorProperty,
                        Duration = 500,
                        To = Color.White
                    }
                    ),
                new VisualState("Orange",
                    new ColorTransition
                    {
                        Property = Window.ColorProperty,
                        Duration = 500,
                        To = PhoneColors.Orange
                    },
                    new ColorTransition
                    {
                        Property = Window.ToolbarColorProperty,
                        Duration = 500,
                        To = PhoneColors.Orange
                    }
                    ),
                new VisualState("Blue",
                    new ColorTransition
                    {
                        Property = Window.ColorProperty,
                        Duration = 500,
                        To = PhoneColors.Blue
                    },
                    new ColorTransition
                    {
                        Property = Window.ToolbarColorProperty,
                        Duration = 900,
                        To = PhoneColors.Blue
                    }
                    ),
                new VisualState("Green",
                    new ColorTransition
                    {
                        Property = Window.ColorProperty,
                        Duration = 500,
                        To = PhoneColors.Green
                    },
                    new ColorTransition
                    {
                        Property = Window.ToolbarColorProperty,
                        Duration = 500,
                        To = PhoneColors.Green
                    }
                    )


                );
            return windowManager;
        }

        private void CreateCustomVisibilityManager()
        {
            VisualStateManager vsm = new VisualStateManager(
                new VisualState(VState.Shown,
                    new FloatTransition { Property = FormBase.VisibilityProperty, From = 0f, To = 1f, Tracking = TrackMode.Keep, EasingFunction = LinearEase.Instance, Duration = 2750 },
                    new FloatTransition { Property = FormBase.TranslateYFactorProperty, From = -1.10f, To = 0f, Tracking = TrackMode.Keep, Duration = 2750, EasingFunction = new ElasticEase { Mode = EasingMode.EaseOut, Oscillations = 5, Springiness = 7d } },
                    new IntTransition { Property = Control.OpacityProperty, From = 0, To = 255, Duration = 500 },
                    new ColorTransition { From = PhoneColors.FromArgb(0, 0, 0, 0), To = PhoneColors.FromArgb(255, 0, 0, 0), Duration = 2000, Delay = 500 }

              )
                , new VisualState(VState.Hidden,
                    new FloatTransition { Property = FormBase.VisibilityProperty, To = 0f, Tracking = TrackMode.Keep, EasingFunction = LinearEase.Instance, Duration = 500 },
                    new FloatTransition { Property = FormBase.TranslateYFactorProperty, To = -1f, Tracking = TrackMode.Keep, EasingFunction = PowerEase.InOut, Duration = 500 },
                    new ColorTransition { To = PhoneColors.FromArgb(0, 0, 0, 0), Duration = 300 }
                    )
             );
            TransitionManager = vsm;
        }

        protected override Shape CreateDefaultBackground()
        {
            return Color.Black;
        }

    }
}
